#include <memory>
//using namespace BuddyAlloc;
#define _ptlong unsigned long

extern _ptlong Kmalloc(_ptlong size);


extern _ptlong Kfree(_ptlong vaddr);


namespace BuddyAllocator
{
  //Code from http://blog.csdn.net/lifesider/article/details/6527788
  
  template<typename T>
  class BuddyAllocator : public std::allocator<T>
  {
    typedef T* pointer;
    // 必须要重新定义，否则容器如 list, set, map 使用时作用域只能到达 std::allocator  
    template<class Other>  
    struct rebind  
    {  
        typedef BuddyAllocator<Other> other;  
    };  

    // 构造函数必须实现  
    BuddyAllocator() {}  
    
    BuddyAllocator(BuddyAllocator<T> const&) {}  
    
    BuddyAllocator<T>& operator=(BuddyAllocator<T> const&) { return (*this); }  
    
    // idiom: Coercion by Member Template  
    template<class Other>  
    BuddyAllocator(BuddyAllocator<Other> const&) {}  
  
    // idiom: Coercion by Member Template  
    template<class Other>  
    BuddyAllocator<T>& operator=(BuddyAllocator<Other> const&) { return (*this); }  
  
    // 内存的分配与释放可以实现为自定义的算法，替换函数体即可.
    pointer allocate(size_t count) { return (pointer)(Kmalloc((_ptlong)count)); }  
  
    void deallocate(pointer ptr, size_t count) { Kfree(ptr); }  
  };
}
